﻿using System;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Input;
using PSSMS.Tools.Utils;

namespace PSSMS
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        BLL.LoginWindow.LoginWindowClient myClient = new BLL.LoginWindow.LoginWindowClient();
        public MainWindow()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 窗口加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //自动登录
           //AutomatiLogin();
        }

        //private void AutomatiLogin()
        //{
        //    throw new NotImplementedException();
        //}

        public static string Txtvalue = string.Empty;
      
        /// <summary>
        /// 立即登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin(object sender, RoutedEventArgs e)
        {
            try
            {
                //*1、判断账号密码不为空；
                if (string.IsNullOrEmpty(txtAccount.Text.Trim()))
                {
                    MessageBox.Show("请检查账号是否为空！", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                if (string.IsNullOrEmpty(pbPassword.Password.Trim()))
                {
                    MessageBox.Show("请检查密码是否为空！", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                // *2、用账号密码作为条件查询员工基本信息，能查到数据则账号正确。
                string strAccount = txtAccount.Text.Trim();
                string strPassword = pbPassword.Password.Trim();
                //调用服务方法
                DataTable dt = myClient.CheckLogin(strAccount, strPassword).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    Window1 mainWindow4 = new Window1();
                    mainWindow4.Show();//关闭登录窗口
                    this.Close();
                }
                else
                {
                    MessageBox.Show("输入账号密码有误", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                //Window1 mainWindow4 = new Window1();
                //mainWindow4.Show();//关闭登录窗口
                //this.Close();
            }
            catch (Exception ex)
            {
                string ex1 = string.Empty;
                // 获取描述当前异常的消息。
                if (ex.InnerException != null)
                {
                    ex1 = ex.InnerException.Message;
                }
                else
                {
                    ex1 = ex.Message;
                    ex1 = ex1.Split('。')[0];
                }
                if (ex1 == "无法连接到远程服务器")
                {
                    MessageBox.Show("服务器处于关闭状态，请启动！", "提 示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else if (ex1 == "在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误")
                {
                    MessageBox.Show("服务端SQL服务处于关闭状态！", "提 示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    MessageBox.Show("您断网了，或服务器处于关闭状态！", "提 示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                Environment.Exit(0);  //中断程序
            }
        }
        /// <summary>
        /// 创建实体类：记录账号信息
        /// </summary>
        [Serializable]
        private class RememberAccount
        {
            public string IsAccount { get; set; }
            public string IsPassword { get; set; }
            public bool IsTheRemember { get; set; }
            public bool IsTheLogin { get; set; }
        }
    }
}
